/**
 * @file  客户\线索 分配
 * @author hj
 */
import Vue from 'vue';
import resource from 'vue-resource';
var nativeApi = require('nativeApi');
import {APIS} from '../../js/config';

Vue.use(resource);

export default {
  // 每个store第一个属性必须为state，state可以保存多个数据状态，state可以被多个组件绑定来同步状态
  state: {
    deptData: {
      'lists': [],
      'selectId': '',
      'name': ''
    },
    personData: {
      'lists': [],
      'selectId': '',
      'name': ''
    },
    params: {}
  },
  getdDeptList: function (params) {
    var self = this;
    self.state.params = params;
    self.state.deptData.selectId = '';
    self.state.deptData.name = '';
    if (self.state.deptData.lists.length > 0) {
      return;
    }
    nativeApi.initAjax({
      type: 'get',
      url: APIS.dept_list,
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data || [];
          lists.map((item) => {
            var deptName = item.deptName || '';
            item.name = deptName.substring(deptName.lastIndexOf('-') + 1, deptName.length);
            item.describeName = deptName.substring(0, deptName.lastIndexOf('-'));
          });
          self.state.deptData.lists = lists;
        } else {
          mui.alert('数据获取失败！', '提示');
        }
      }
    });
  },
  getPersonList: function (deptId, cb) {
    var self = this;
    self.state.personData.selectId = '';
    self.state.personData.name = '';
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.dept_emp_list.replace('{id}', deptId),
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data;
          if (lists.length === 0) {
            mui.alert('该部门没有员工！', '提示');
            return;
          }
          self.state.personData.lists = lists;
          if (typeof cb === 'function') {
            cb();
          }
        } else {
          mui.alert(result.msg || '数据获取失败！', '提示');
        }
      }
    });
  },
  distributionFn: function (field) {
    var params = this.state.params;
    var self = this;
    var param = {
      'toDeptId': 0,
      'toStaffId': 0,
      'assignType': 3 // 线索池
    };
    if (field == 'dept') {
      param.toDeptId = this.state.deptData.selectId;
      param.assignReason = '分配给' + this.state.deptData.name;
      param.assignType = 0;
    } else {
      param.assignReason = '分配给' + this.state.personData.name;
      param.toStaffId = this.state.personData.selectId;
      param.assignType = 1;
    }
    if (params.source === 'customer') {
      // 客户分配
      nativeApi.initAjax({
        type: 'post',
        url: APIS.customerDetail + params.id + '/assignto',
        param: param,
        callback: function (result) {
          if (result && result.code === 200) {
            if (field == 'dept') {
              window.history.go(-1);
            } else {
              window.history.go(-2);
            }
            setTimeout(function () {
              window['customerBack']();
            }, 50);
          } else {
            mui.alert(result.msg || '请求失败！');
          }
        }
      });
    } else if (params.source === 'clue') {
      // 线索分配
      if (!(param.toDeptId || param.toStaffId)) {
        param.assignReason = '分配到线索池';
      }
      nativeApi.initAjax({
        type: 'post',
        url: APIS.clue + '/' + params.id + '/assignto',
        param: param,
        callback: function (result) {
          if (result && result.code === 200) {
            if (field == 'dept') {
              window.history.go(-1);
            } else {
              window.history.go(-2);
            }
            setTimeout(function () {
              window['clueBack']();
            }, 50);
          } else {
            mui.alert(result.msg || '请求失败！', '提示');
          }
        }
      });
    }
  }
};
